var _tab = null;

$(function(){
	mouseContext();
	
	//布局
	$("#layout1").ligerLayout({
		height: '100%',
		topHeight:26,
		heightDiff: -2,
		space:2,
		onHeightChanged: function(options){
			if (_tab){
		        _tab.addHeight(options.diff);
		    }
		}
	});
	
	//Tab
	var height = $(".l-layout-center").height();
	$("#framecenter").ligerTab({ height: height });
    _tab = $("#framecenter").ligerGetTabManager();
	
	/* 创建菜单栏 start */
	var menuJson = {
    	"classname": "com.wxtf.platform.plugins.permissions.PermissionsAction",
		"methodname": "loadMenu4Left1",
		"logicdata": [],
		"extenddata": []
    };
    
    var menus = new Array();
	callserver(menuJson, function(data, status){
    	if(data.length > 0){
	    	for(var i = 0; i < data.length; i++){
	    		var json = data[i].items.items;
	    		changeCikckEvent(json);
	    		menus.push({ id:data[i].id, text: data[i].title, menu: data[i].items });
	    	}
	    	
	    	//创建菜单栏
			$("#topMenuDiv").ligerMenuBar({
				items: menus
			});
    	}
    }, function(data, status){
    	
    });
	/* 创建菜单栏 end */
	
    $("#pageloading").hide();
});

/*
 * tab页添加
 */
function f_addTab(tabid,text, url){
	if(_tab.getTabItemCount() > _maxTab){
		var tabArray = _tab.getTabidList ('home', true);
		_tab.removeTabItem(tabArray[0]);
	}
    _tab.addTabItem({ tabid: tabid,text: text, url: url });
}

/*
 * tab页移除
 */
function removeTab(tabid){
	_tab.removeTabItem(tabid);
}

/*
 * 修改菜单点击事件
 */
function changeCikckEvent(jsonArray){
	if(jsonArray.length > 0){
		for(var i = 0; i < jsonArray.length; i++){
			var json = jsonArray[i];
			if(json.children){
				changeCikckEvent(json.children);
			} else {
				json['click'] = menuClick;
			}
		}
	}
}

/*
 * 菜单点击事件
 */
function menuClick(node){
	if (!node.url){
		succMsgTip("该菜单没有设置访问路径");
		return;
	}
	var tabid = $(node.target).attr("tabid");
	if (!tabid){
	    tabid = '_tab_' + node.id;
	    $(node.target).attr("tabid", tabid)
	}
	if(node.types == 'iframe'){
		f_addTab(tabid, node.text, node.url);
	} else if(node.types == 'newPage'){
		window.open(node.url, node.text);
	} else {
		succMsgTip("菜单类型非法");
	}
}


/*
 * 收缩/伸展Logo
 */
function CollapseLogo(ele){
	var flag = ele.src;
	if(ele.src.endWith('up.gif')){//收缩
		ele.src = 'resource/images/mbi_down.gif';
		document.getElementById('logoDiv').style.display = 'none';
		
		var height = $(".l-layout-center").height() + 75;
		_tab.addHeight(75);
		$(".l-layout-center").css({height: height});
	} else if(ele.src.endWith('down.gif')){//伸展
		ele.src = 'resource/images/mbi_up.gif';
		document.getElementById('logoDiv').style.display = 'block';
		
		var height = $(".l-layout-center").height() - 75;
		_tab.addHeight(-75);
		$(".l-layout-center").css({height: height});
	}
}

/*
 * 鼠标右键
 */
function mouseContext(){
	$('body').live("mousedown",function(e){
		$('body').bind("contextmenu",function(e){ 
	         return false; 
	    }); 
		
		if(e.which != 1){
			return false;
		}
	});
}

/*
 * 打开快捷菜单
 */
function openQuickMenu(id, name, url, types){
	if(types == 'newPage'){
		window.open(url);
	} else if(types == 'iframe'){
		var tabId = '_tab_' + id;
		f_addTab(tabId, name, url);
	} else {
		$.ligerDialog.open({ height: 200, width: 400, url: url });
	}
}

/* 切换机构与切换样式 start */
var winOrgChange ;
var winCssChange ;
function showOrgWin(){
	if(winOrgChange==null){
		winOrgChange = initWindowByDiv('orgChange','切换机构', 300, 200,controlShowOrgWin,function (){winOrgChange.hide();});
	}else{
		winOrgChange.show();
	}
}
function showCssWin(){
	if(winCssChange==null){
		winCssChange = initWindowByDiv('cssChange','切换皮肤', 300, 200,controlShowCssWin,function (){winCssChange.hide();});
	}else{
		winCssChange.show();
	}
}

function controlShowOrgWin(){
	var obj = document.getElementById("updateOrg");
	var id = $('#updateOrg').val();
	var text = obj.options[obj.selectedIndex].text;
	var jsonObj = { 
		"classname": "com.wxtf.platform.plugins.user.UserLoginAction",
		"methodname": "updateOrg",
		"logicdata": "[{\"userOrg\":\"" + (id + "_" + text) + "\"}]",
		"extenddata": "[]"
	};
	callserver(jsonObj, function(){
		winOrgChange.hide();
		window.location.reload();
		succMsgTip('用户机构切换成功！');
	},
	function(){
		succMsgTip('用户机构切换有误，请重试！');
	});	
}
function controlShowCssWin(){
	var jsonObj = { 
		"classname": "com.wxtf.platform.plugins.user.UserAction",
		"methodname": "updateCss",
		"logicdata": "[{\"userCss\":\"" + $('#updateCss').val() + "\"}]",
		"extenddata": "[]"
	};
	callserver(jsonObj,function(){
		winCssChange.hide();
		window.location.reload();	
		succMsgTip('用户皮肤切换成功！');
	},function(){
		succMsgTip('用户皮肤切换有误，请重试！');
	});
}
/* 切换机构与切换样式 end */

/*
 * 退出系统
 */
function exitBye(){
	$.ligerDialog.confirm('确定要退出系统吗？', function (ok) {
		if(ok){
			var jsonObj = { 
				"classname": "com.wxtf.platform.plugins.user.UserAction",
				"methodname": "logout",
				"logicdata": "[]",
				"extenddata": "[]"
			};
			callserver(jsonObj, function(data, satus){
				window.location = _basePath;
			},function(data, satus){
				succMsgTip(data.resultrecord);
			});
		}
	});
}

/*
 * 全屏
 */
function fullScreen(ele){
	if(ele.value == 'n'){//初始状态，可以全屏操作
		ele.value = 'y';
	}else{//已经全屏，只能回到初始
		ele.value = 'n';
	}
	
}










